//
//  HJNewFeatureViewController.swift
//  Micro-blog Sina
//
//  Created by admin on 16/3/22.
//  Copyright © 2016年 admin. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class HJNewFeatureViewController: UICollectionViewController {
    
    // Layout attribute
    let layout = UICollectionViewFlowLayout()
    
    init() {
        super.init(collectionViewLayout: layout)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        prepareCollectionView()
        
    }
    private func prepareCollectionView() {
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
        self.collectionView!.registerClass(HJNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }
}

// MARK: - UICollectionViewDataSource,UICollectionViewDelegate
extension HJNewFeatureViewController {
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return HJNewFeatuerPageCount
    }
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! HJNewFeatureCell
        cell.index = indexPath.item
        return cell
    }
    
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / UIScreen.width)
        print("page = \(page)")
        if page == HJNewFeatuerPageCount - 1{
            // Create indexPath
            let indexPath = NSIndexPath(forItem: page, inSection: 0)
            // Acquisition of indexPath corresponding to the cell
            let cell = collectionView?.cellForItemAtIndexPath(indexPath) as! HJNewFeatureCell
            cell.showStartButton()
        }
    }
}

class HJNewFeatureCell: UICollectionViewCell {
    // MARK: - 属性
    var index: Int = 0 {
        didSet{
            backgroundImageView.image = UIImage(named: "new_feature_\(index + 1)")
            // As long as the cell to set the background picture, hidden button
            startButton.hidden = true
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }
    
    // PrepareUI
    private func prepareUI() {
        contentView.addSubview(backgroundImageView)
        contentView.addSubview(startButton)
        
        backgroundImageView.translatesAutoresizingMaskIntoConstraints = false
        startButton.translatesAutoresizingMaskIntoConstraints = false
        
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[bg]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bg" : backgroundImageView]))
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[bg]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bg" : backgroundImageView]))
        
        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -120))
    }
    // showStartButton
    func showStartButton() {
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1, delay: 0.05, usingSpringWithDamping: 0.6, initialSpringVelocity: 1, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                print("开始体验 动画结束")
        }
        
    }
    // MARK: - StartButton Event
    func startButtonClick() {
        AppDelegate.outSwitchRootViewController(true)
    }
    
    // MARK: - Lazy load
    private lazy var backgroundImageView = UIImageView()
    private lazy var startButton: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.setTitle("开始体验", forState: UIControlState.Normal)
        button.addTarget(self, action: "startButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
}
